// ==UserScript==
// @name         New Userscript
// @namespace    http://tampermonkey.net/
// @version      2025-02-03
// @description  try to take over the world!
// @author       You
// @match        https://chat.deepseek.com/a/chat/s/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=deepseek.com
// @require      https://update.greasyfork.org/scripts/498507/1398070/sweetalert2.js
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    const originalOpen = XMLHttpRequest.prototype.open;

    // 重写 XMLHttpRequest.prototype.open 函数
    XMLHttpRequest.prototype.open = function (method, url, async, user, password) {
        // 检查是否为需要监听的特定URL
        const targetSubstrings = ['chat/history_messages'];
        if (targetSubstrings.some(substring => url.includes(substring))) {
            // 继续使用原始的 open 方法
            originalOpen.apply(this, arguments);

            // 添加 readystatechange 事件监听器
            this.addEventListener('readystatechange', function () {
                // 当请求完成时
                if (this.readyState === 4 && this.status >= 200 && this.status < 300) { // 确保响应成功
                    console.log('Response received:', this.responseText);
                }
            }, false);
        } else {
            // 对于其他URL，直接调用原始的 open 方法，不添加额外逻辑
            originalOpen.apply(this, arguments);
        }
    };
    // Your code here...
    window.addEventListener('load', addPanel);


})();

function addPanel(){
    function genButton(text, foo, id, fooParams = {}){
        let b = document.createElement('button');
        b.textContent = text;
        b.style.verticalAlign = 'inherit';
        // 使用箭头函数创建闭包来保存 fooParams 并传递给 foo
        b.addEventListener('click', () => {
            foo.call(b, ...Object.values(fooParams)); // 使用 call 方法确保 this 指向按钮对象
        });
        if(id){ b.id = id };
        return b;
    }

    function changeRangeDynamics() {
        const value = parseInt(this.value, 10);
        const roundedValue = Math.ceil(value / 10) * 10;
    
        targetAmountGlobal = roundedValue;
        // 只能通过 DOM 方法改变
        document.querySelector('#swal-range > output').textContent = roundedValue;
    }

    async function openPanelFunc(){
        let isLoadEnd = false;
        targetAmountGlobal = 100;
        var swalRangeValue = 100;
        setTimeout(() => {
            isLoadEnd = true;
        }, 13000);
        const { value: formValues } = await Swal.fire({
            title: "最大点赞数",
            showCancelButton: true,
            cancelButtonText: '取消',
            confirmButtonText: '确定',
            //class="swal2-range" swalalert框架可能会对其有特殊处理，导致其内标签的id声明失效
            html: `
              <div class="swal2-range" id="swal-range" style="display: flex;">
                <input type="range" min="0" max="500" step="10" value="100">
                <output>${swalRangeValue}</output>
              </div>
            `,
            focusConfirm: false,
            didOpen: () => {
                const swalRange = document.querySelector('#swal-range input');
                swalRange.addEventListener('input', changeRangeDynamics);
            },
            willClose: () => {
                // 在关闭前清除事件监听器以防止内存泄漏
                const swalRange = document.querySelector('#swal-range input');
                swalRange.removeEventListener('input', changeRangeDynamics);
            },
            preConfirm: () => {
              return [
                targetAmountGlobal
              ];
            }
        });
        if (formValues) {
            targetAmountGlobal = formValues[0];
            clickCommentsAuto(targetAmountGlobal);
        }
    }

    let myButton = genButton('CMlike', openPanelFunc, 'CMlike');
    document.body.appendChild(myButton);

    var css_text =`
        #CMlike {
            position: fixed;
            color: rgb(211, 67, 235);
            top: 70%;
            left: -20px;/* 初始状态下左半部分隐藏 */
            transform: translateY(-50%);
            z-index: 1000; /* 确保按钮在最前面 */
            padding: 10px 24px;
            border-radius: 5px;
            cursor: pointer;
            border: 0;
            background-color: white;
            box-shadow: rgb(0 0 0 / 5%) 0 0 8px;
            letter-spacing: 1.5px;
            text-transform: uppercase;
            font-size: 9px;
            transition: all 0.5s ease;
        }
        #CMlike:hover {
            left: 0%; /* 鼠标悬停时完整显示 */
            letter-spacing: 3px;
            background-image: linear-gradient(to top, #fad0c4 0%, #fad0c4 1%, #ffd1ff 100%);
            box-shadow: rgba(211, 67, 235, 0.7) 0px 7px 29px 0px; /* 更柔和的紫色阴影，带透明度 */
        }
        
        #CMlike:active {
            letter-spacing: 3px;
            background-image: linear-gradient(to top, #fad0c4 0%, #fad0c4 1%, #ffd1ff 100%);
            box-shadow: rgba(211, 67, 235, 0.5) 0px 0px 0px 0px; /* 活动状态下的阴影，保持一致性 */
            transition: 100ms;
        }
    `
    GMaddStyle(css_text);
}

function GMaddStyle(css){
    var myStyle = document.createElement('style');
    myStyle.textContent = css;
    var doc = document.head || document.documentElement;
    doc.appendChild(myStyle);
}